import Vue from "vue"
import VueRouter from "vue-router"
import Layout from "@/views/Layout/Layout"
import MainRouter from "@/views/Layout/MainRouter"
import Login from "@/views/Login/login"

import staticRoutes from "./static-route.js"

import Home from "@/views/Home/Home"
import PlaceHolder from "@/views/Home/PlaceHolder"
import store from "../store"

//异步
const Advert = () => import("@/views/Advert/Advert")
const Pmjl = () => import("@/views/Pmjl/Pmjl")
const Ylyc = () => import("@/views/Ylyc/Ylyc")
const PmjlTable = () => import("@/views/Pmjl/PmjlTable")

const PmjlEcharts = () => import("@/views/Pmjl/PmjlEcharts")
const YlycEcharts = () => import("@/views/Ylyc/YlycEcharts")
const KxylEcharts = () => import("@/views/KxylEaton/KxylEcharts")
const TtylEcharts = () => import("@/views/Ttyl/TtylEcharts")

const MonPmjl = () => import("@/views/Pmjl/MonPmjl")
const AddZndb = () => import("@/views/Pmjl/AddZndb.vue")
const Order = () => import("@/views/Order/Order")
const Params = () => import("@/views/Params/Params")
const User = () => import("@/views/User/User")
const Menu = () => import("@/views/Menu/menu")
const CurveData = () => import("@/views/Sjdr/CurveData")
const NewProject = () => import("@/views/ProjectManage/NewProjectDialog.vue")
const WellInfo = () => import("@/views/ProjectManage/WellInfo.vue")
const Jsyl = () => import("@/views/Jsyl/Jsyl.vue")
const YxData = () => import("@/views/Sjdr/YxData.vue")
const FcData = () => import("@/views/Sjdr/FcData.vue")
const QtData = () => import("@/views/Sjdr/QtData.vue")
const WisData = () => import("@/views/Sjdr/WisData.vue")
const WbData = () => import("@/views/Sjdr/WbData.vue")
const PreDataKxylEcharts = () => import("@/views/KxylEaton/PreDataKxylEcharts.vue")
const PreKxylEcharts = () => import("@/views/KxylEaton/PreKxylEcharts.vue")
const JsylMd = () => import("@/views/Jsfx/NhgJs.vue")
const SfdcYl = () => import("@/views/Jsfx/ObgJs.vue")
const EatonJz = () => import("@/views/Jsfx/EatonJz.vue")
const JsfxIndexPage = () => import("@/views/Jsfx/IndexPage.vue")
const BowersMethod = () => import("@/views/Jsyl/BowersMethod.vue")
const BowersZs = () => import("@/views/Jsfx/BowersZs.vue")

const ElementCrossplotMgk = () => import("@/views/Dashboard/ElementCrossplot.vue")

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    name: "root",
    redirect: "/login",
    component: Layout,
    children: [
      {
        path: "/home",
        name: "Home",
        component: Home
      },
      {
        path: "/advert",
        name: "Advert",
        component: Advert
      },
      {
        path: "/pmjl",
        name: "Pmjl",
        component: Pmjl
      },
      {
        path: "/PmjlTable",
        name: "PmjlTable",
        component: PmjlTable
      },
      {
        path: "/PmjlEcharts",
        name: "PmjlEcharts",
        component: PmjlEcharts
      },
      {
        path: "/YlycEcharts",
        name: "YlycEcharts",
        component: YlycEcharts
      },
      {
        path: "/KxylEcharts",
        name: "KxylEcharts",
        component: KxylEcharts
      },
      {
        path: "/TtylEcharts",
        name: "TtylEcharts",
        component: TtylEcharts
      },
      {
        path: "/monPmjl",
        name: "MonPmjl",
        component: MonPmjl
      },
      {
        path: "/ylyc",
        name: "Ylyc",
        component: Ylyc
      },
      {
        path: "/addZndb",
        name: "AddZndb",
        component: AddZndb
      },
      {
        path: "/order",
        name: "Order",
        component: Order
      },
      {
        path: "/params",
        name: "Params",
        component: Params
      },
      {
        path: "/user",
        name: "User",
        component: User
      },
      ,
      {
        path: "/menu",
        name: "Menu",
        component: Menu
      },
      {
        path: "/curveData",
        name: "CurveData",
        component: CurveData
      },
      {
        path: "/newProject",
        name: "NewProject",
        component: NewProject
      },
      {
        path: "/wellInfo",
        name: "WellInfo",
        component: WellInfo
      },
      {
        path: "/Jsyl",
        name: "Jsyl",
        component: Jsyl
      },
      {
        path: "/yxData",
        name: "YxData",
        component: YxData
      },
      {
        path: "/fcData",
        name: "FcData",
        component: FcData
      },
      {
        path: "/qtData",
        name: "QtData",
        component: QtData
      },
      {
        path: "/wisData",
        name: "WisData",
        component: WisData
      },
      {
        path: "/wbData",
        name: "WbData",
        component: WbData
      },
      {
        path: "/preKxylEcharts",
        name: "PreKxylEcharts",
        component: PreDataKxylEcharts
      },
      // {
      //   path: "/preKxylEcharts",
      //   name: "PreKxylEcharts",
      //   component: PreKxylEcharts
      // },
      {
        path: "/jsylMd",
        name: "JsylMd",
        component: JsylMd
      },
      {
        path: "/sfdcYl",
        name: "SfdcYl",
        component: SfdcYl
      },
      {
        path: "/eatonJz",
        name: "EatonJz",
        component: EatonJz
      },
      {
        path: "/IndexPage",
        name: "IndexPage",
        component: JsfxIndexPage
      },
      {
        path: "/bowersMethod",
        name: "BowersMethod",
        component: BowersMethod
      },
      {
        path: "/bowersZs",
        name: "BowersZs",
        component: BowersZs
      }
    ]
  },
  {
    path: "/login",
    name: "Login",
    component: Login
  }
]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
})

// 动态加载视图组件
function loadView(view) {
  try {
    return () => import(`@/views/${view}`)
  } catch (error) {
    return PlaceHolder
  }
}

// 将后端菜单转换为路由配置
export function convertMenusToRoutes(menus, parentName) {
  const hasChildren = []
  for (const menu of menus) {
    const route = {
      path: "/" + menu.path,
      name: menu.label,
      component: !menu.url || menu.url == "Layout" ? MainRouter : loadView(menu.url),
      meta: { icon: menu.icon }
    }

    router.addRoute(parentName, route)

    const children = menu.children || []
    if (children.length) hasChildren.push(menu)
  }

  for (const it of hasChildren) {
    convertMenusToRoutes(it.children, it.label)
  }
}

store.commit("setMenus", staticRoutes)
sessionStorage.setItem("dynamic_menus", JSON.stringify(staticRoutes))
convertMenusToRoutes(staticRoutes, "root")

let loaded = false
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token")
  if (!token && to.name !== "Login") {
    // 确保不是登录页面的路由
    next({ name: "Login" }) // 重定向到登录页面
  } else if (to.name == "Login") {
    next(true)
  } else {
    next()
  }
})

export default router
